package showtime.framework.utils
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	
	import showtime.framework.display.TileBitmapData;

	
	/** 
	 * @project showtime
	 * @author yinhao
	 * @date 2011-1-29
	 *
	 */
	
	public final class GraphicUtility
	{
		public function GraphicUtility()
		{
			
		}
		
		/**
		 * 该方法用来获取位图有效显示区域，最小的矩形范围。
		 *  
		 * @param source
		 * @return 
		 * 
		 */		
		static public function getAvailableRect(source:BitmapData):BitmapData
		{
			var rec:Rectangle = source.getColorBoundsRect(0xFF000000, 0x00000000, false);
			
			var buffer:BitmapData = new BitmapData(rec.width, rec.height, true, 0xFF);
			
			var mat:Matrix = new Matrix(1, 0, 0, 1, -rec.x, - rec.y);
			
			buffer.draw(source, mat);
			
			return buffer;
		}
		
		static public function getHorizontalReverseRect(source:BitmapData):BitmapData
		{
			var newPixels:BitmapData = new BitmapData(source.width, source.height, true, 0x00000000);
			
			var mtx:Matrix = new Matrix();				
			mtx.scale(-1, 1);
			
			mtx.translate(newPixels.width, 0);
			newPixels.draw(source, mtx);
			
			return newPixels;
		}
		
		/**
		 * The source displayObject ,which you want to copy. 
		 * @param source
		 * @return 
		 * 
		 */		
		static public function copyAsBitmapData(source:DisplayObject):BitmapData
		{
			var bitmapData:BitmapData = new BitmapData(source.width, source.height, true, 0x00ffffff);
			bitmapData.draw(source);
			
			return bitmapData;
		}
		
		/**
		 * BMP source file, bmp row amount, bmp col amount. 
		 * @param source
		 * @param tileWidth
		 * @param tileHeight
		 * @return 
		 * 
		 */		
		static public function convertToTile(source:*, tileWidth:int, tileHeight:int):Object
		{	
			
			var rowCount:int = source.width / tileWidth;
			
			var colCount:int = source.height / tileHeight;
			
			var resultMap:Object = {};
			
			var matrix:Matrix = new Matrix();
			
			var copyRect:Rectangle = new Rectangle(0, 0, tileWidth, tileHeight);
			
			var id:int = 0;
			
			for (var j:int = 0; j < colCount; j++)
			{
				for (var i:int = 0; i < rowCount; i++)
				{
					var bmp:TileBitmapData = new TileBitmapData(tileWidth, tileHeight, true, 0x00000000);
					
					bmp.id = ++id;
					
					bmp.offsetX = i * tileWidth;
					
					bmp.offsetY = j * tileHeight;
					
					matrix.tx = -i * tileWidth;
					
					matrix.ty = -j * tileHeight;
					
					bmp.draw(source, matrix, null, null, copyRect, true);
					
					resultMap[bmp.id] = bmp;
				}
				
			} 
			
			return resultMap;
		}
		
		/**
		 * BMP source file, bmp row amount, bmp col amount.
		 *  
		 * @param source
		 * @param row
		 * @param col
		 * @param total
		 * @return 
		 * 
		 */		
		static public function convertToFrame(source:*, row:int, col:int):Array
		{
			var width:int = source.width / row;
			
			var height:int = source.height / col;
			
			var resultArray:Array = [];
			
			var matrix:Matrix = new Matrix();
			
			var copyRect:Rectangle = new Rectangle(0, 0, width, height);
			
			for (var j:int = 0; j < col; j++)
			{
				var temp:Array = [];
				
				for (var i:int = 0; i < row; i++)
				{
					var bmp:BitmapData = new BitmapData(width, height, true, 0x00000000);
					
					matrix.tx = -i * width;
					
					matrix.ty = -j * height;
					
					bmp.draw(source, matrix, null, null, copyRect, true);
					
					temp.push(bmp);
				}
				
				resultArray.push(temp);
			}
			
			return resultArray;
		}
		
		/**
		 * 
		 * @param graphic
		 * @return 
		 *  
		 */		
		static public function rightAndLeft(graphic:BitmapData):BitmapData
		{
			var bmd:BitmapData = new BitmapData(graphic.width, graphic.height, true, 0x00000000);
			
			for (var yy:int=0; yy < graphic.height; yy++)
			{
				for (var xx:int=0; xx < graphic.width; xx++)
				{
					bmd.setPixel32(graphic.width - xx - 1, yy, graphic.getPixel32(xx, yy));
				}
			}
			
			return bmd;
		}
		
		
		/**
		 *  
		 * @param source The source bmp of number image.
		 * @param value The value you want to converted.
		 * @param perWidth One unit width of Number you offer.
		 * @param perHeight One unit height of Number you offer.
		 * @param useFlag Is use flag.
		 * 
		 */		
		static public function numberToGraphic(source:BitmapData, value:int, perWidth:int, perHeight:int, useFlag:Boolean = false):Shape
		{	
			var container:Shape = new Shape();
			
			// 偏移矩阵。
			var mix:Matrix = new Matrix();		
			
			// 循环体。			
			var numbers:Array = String(value).split("");
			
			if (useFlag)
			{
				container.graphics.beginBitmapFill(source, mix);
				container.graphics.drawRect(0, 0, perWidth, perHeight);
				
				for (var index:int = 0; index < numbers.length; index++)
				{
					mix.tx = (index - numbers[index]) * perWidth;
					
					container.graphics.beginBitmapFill(source, mix);
					
					container.graphics.drawRect((index + 1) * perWidth, 0, perWidth, perHeight);
				}
			}
			else
			{
				for (index = 0; index < numbers.length; index++)
				{
					mix.tx = (index - numbers[index]) * perWidth;
					
					container.graphics.beginBitmapFill(source, mix);
					
					container.graphics.drawRect(index * perWidth, 0, perWidth, perHeight);
				}
			}		
			
			container.graphics.endFill();
			
			return container;
		}
	}
}